﻿package com.athena.controller 
{
	import com.athena.vo.Chess;
	import com.athena.process.*;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import com.adobe.serialization.json.JSON;
	/**
	 * ...
	 * @author Tamnt
	 */
	public class ChessController extends MovieClip
	{
		public var mc:MovieClip;
		public var objC:Chess;		
		public function ChessController(_mc:MovieClip,_obj:Chess) 
		{
			mc = _mc ;
			objC = _obj;
			mc.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
			//mc.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
			//mc.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			mc.addEventListener(MouseEvent.CLICK, mouseClick);
		}
		
		function mouseOver(evt:MouseEvent)
		{
			//if (objC.Color == Cocaro.MyColor)
				//mc.gotoAndStop(2) ;
		}
		
		
		function mouseClick(evt:MouseEvent)
		{
			//Cocaro.tf.appendText("Color:" + objC.Color + "-" + Cocaro.MyColor) ;
			if (ProcessInTable.currTable.Status != 1) return ;
			if (ProcessInTable.currentTimerRun != ProcessInTable.currPosition) return ;
			if (objC.Color == ProcessInTable.MyColor)
				mc.gotoAndStop(2) ;			
			//Cocaro.tf.appendText("Selected") ;
			//for (var n:int = 0; n < Cocaro.currTable.ArrPlayers[Cocaro.csOther].ArrChess.length; n++) 
			//{
				//Cocaro.currTable.ArrPlayers[Cocaro.csOther].ArrChess_Movie[n].gotoAndStop(1) ;
			//}			
			if (objC.Color == ProcessInTable.MyColor) { //Chon dung cay cua minh
				for (var i:int = 0; i < ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess.length; i++) 
				{
					if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[i].Symbol != objC.Symbol) || (ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[i].Stt != objC.Stt)){
						if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess_Movie[i].currentFrame != 1)
							ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess_Movie[i].gotoAndStop(1) ;				
					}
				}
				for (var j:int = 0; j < 10; j++) 
				{
					for (var k:int = 0; k < 9; k++) 
					{
						Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].visible = false ;
					}
				}
				for (var l:int = 0; l < ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie.length; l++) 
				{
					if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[l] != 1)
						ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[l].gotoAndStop(1) ;				
				}
				if (objC.Symbol == "X")
					X_Controller.Valid(objC) ;
				else if (objC.Symbol == "M")
					M_Controller.Valid(objC) ;
				else if (objC.Symbol == "P")
					P_Controller.Valid(objC) ;
				else if (objC.Symbol == "T")
					T_Controller.Valid(objC) ;	
				else if (objC.Symbol == "S")
					S_Controller.Valid(objC) ;	
				else if (objC.Symbol == "Tg")
					Tg_Controller.Valid(objC) ;
				else if (objC.Symbol == "B")
					B_Controller.Valid(objC) ;
				ProcessInTable.chessSelected = objC ;
				ProcessInTable.chessMove = mc ;
				ProcessInTable.demClick = 0 ;
			} else {
				if ((ProcessInTable.chessSelected != null) && (ProcessInTable.currTable.Status == 1)) {				
					var csOld : int = 0 ;
					for (var n:int = 0; n < ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess.length; n++) 
					{
						if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[n].Symbol == ProcessInTable.chessSelected.Symbol)
							&& (ProcessInTable.currTable.ArrPlayers[ProcessInTable.currPosition].ArrChess[n].Stt == ProcessInTable.chessSelected.Stt)) {
							csOld = n ;
							break ;
						}
					}
					//Cocaro.tf.appendText("==> An quan1:" + objC.Symbol + "-" + objC.Stt + "-" + objC.Color) ;
					for (var m:int = 0; m < ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess.length; m++) 
					{
						//Cocaro.tf.appendText("Value:" + Cocaro.ArrChess1[m].Symbol + "-" + Cocaro.ArrChess1[m].Stt + "-" + objC.Symbol + "-" + objC.Stt) ;
						if ((ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[m].Symbol == objC.Symbol) && (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[m].Stt == objC.Stt)){
							//Cocaro.tf.appendText("Frame123:" + Cocaro.currTable.ArrPlayers[Cocaro.csOther].ArrChess_Movie[m].currentFrame) ;
							if (ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess_Movie[m].currentFrame == 3) {
								if (ProcessInTable.ForgedChess_Play(ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[m].Hor, ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[m].Ver
																	,ProcessInTable.chessSelected.Hor, ProcessInTable.chessSelected.Ver, csOld)) {
									//Gui len Server
									var obj : Object = new Object() ;
									obj.S = ProcessInTable.chessSelected.Symbol ;
									obj.Stt = ProcessInTable.chessSelected.Stt ;
									obj.H = ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[m].Hor ;
									obj.V = ProcessInTable.currTable.ArrPlayers[ProcessInTable.csOther].ArrChess[m].Ver ;
									obj.N = Cocaro.currPlayer.PlayerName ;
									obj.evt = "playchess";
									Cocaro.mainSocket.sendData(JSON.encode(obj)) ;									
								}								
								//ExternalInterface.call("sendEventToServer","playchess",obj);
								break ;
							}
						}
					}
				}
			}
			//Display lại giao diện bàn chơi
		}
	}
}